<admintpl file="header"/>
<style type="text/css">
    .pic-list li {
        margin-bottom: 5px;
    }
</style>
<script type="text/html" id="photos-item-wrapper">
    <li id="savedimage{id}">
        <input id="photo-{id}" type="hidden" name="photos_url[]" value="{filepath}">
        <img id="photo-{id}-preview" src="{url}" style="height:36px;width: 36px;"
             onclick="parent.image_preview_dialog(this.src);">
        <a href="javascript:upload_one_image('图片上传','#photo-{id}');">替换</a>
        <a href="javascript:(function(){$('#savedimage{id}').remove();})();">移除</a>
    </li>
</script>
</head>
<body>
<div class="wrap js-check-wrap">
    <ul class="nav nav-tabs">
        <li><a href="{:U('PromotionCoupon/index')}">优惠券列表</a></li>
        <li class="active"><a href="{:U('PromotionCoupon/add')}">添加优惠券</a></li>
    </ul>
    <form action="{:U('PromotionCoupon/add_post')}" method="post" class="form-horizontal js-ajax-forms"
          enctype="multipart/form-data">
        <div class="row-fluid">
            <div class="span9">
                <table class="table table-bordered">
                    <tr>
                        <th width="80">优惠券名称</th>
                        <td>
                            <input type="text" style="width:400px;" name="coupon_name" id="coupon_name" required value=""
                                   placeholder="请输入优惠券名称"/>
                            <span class="form-required">*</span>
                        </td>
                    </tr>
                    <tr>
                        <th width="80">一级标题</th>
                        <td>
                            <input type="text" style="width:400px;" name="title" id="title" required value=""
                                   placeholder="请输入标题"/>
                            <span class="form-required">*</span>
                        </td>
                    </tr>
                    <tr>
                        <th width="80">二级标题</th>
                        <td>
                            <input type="text" style="width:400px;" name="title2" id="title2" required value=""
                                   placeholder="请输入二级标题"/>
                            <span class="form-required">*</span>
                        </td>
                    </tr>
                    <tr>
                        <th>商户</th>
                        <td>
                            <select name="merchant_id" id="merchant_id">
                                <foreach name="merchants" item="vo">
                                    <option value="{$vo.id}">{$vo.name}</option>
                                </foreach>
                            </select>
                            <span class="form-required">*</span>
                        </td>
                    </tr>
                    <tr>
                        <th>有效期</th>
                        <td>
                            <input type="text" name="start_date" value="{:date('Y-m-d',time())}"
                                   class="js-datetime" style="width: 160px;"> -
                            <input type="text" name="end_date" value="{:date('Y-m-d',time())}"
                                   class="js-datetime" style="width: 160px;">
                            <span class="form-required">*</span>
                        </td>
                    </tr>
                    <tr>
                        <th>使用限制</th>
                        <td>
                            <input type="text" name="use_limit" id="use_limit" value="" style="width: 400px"
                                   placeholder="请填写使用限制说明">
                            <span class="form-required">*</span>
                        </td>
                    </tr>
                    <tr>
                        <th>优惠限额</th>
                        <td>
                            <input type="text" name="discount_limit" id="discount_limit" value="" style="width: 400px"
                                   placeholder="请填写优惠限额说明">
                            <span class="form-required">*</span>
                        </td>
                    </tr>

                    <tr>
                        <th>发放数量</th>
                        <td>
                            <input type="text" name="total_num" id="total_num" value="" style="width: 100px"
                                   placeholder="请填写发放数量" required >
                            <span class="form-required">*</span>
                        </td>
                    </tr>
                    <tr>
                        <th>换购积分</th>
                        <td>
                            <input type="text" name="exintegral" id="exintegral" value="" style="width: 100px"
                                   placeholder="请填写换购积分" required >
                            <span class="form-required">*</span>
                        </td>
                    </tr>
                    <tr>
                        <th>优惠券详细说明</th>
                        <td>
                            <script type="text/plain" id="content" name="description"></script>
                        </td>
                    </tr>
                    <tr>
                        <th>优惠轮播图</th>
                        <td>
                            <ul id="photos" class="pic-list unstyled"></ul>
                            <a href="javascript:upload_multi_image('图片上传','#photos','photos-item-wrapper');"
                               class="btn btn-small">选择图片</a>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="span3">
                <table class="table table-bordered">
                    <tr>
                        <th><b>展示图</b></th>
                    </tr>
                    <tr>
                        <td>
                            <div style="text-align: center;">
                                <input type="hidden" name="promotion_img" id="promotion_img" value="">
                                <a href="javascript:upload_one_image('图片上传','#promotion_img');">
                                    <img src="__TMPL__Public/assets/images/default-thumbnail.png" id="promotion_img-preview"
                                         width="135" style="cursor: hand"/>
                                </a>
                                <input type="button" class="btn btn-small"
                                       onclick="$('#promotion_img-preview').attr('src','__TMPL__Public/assets/images/default-thumbnail.png');$('#promotion_img').val('');return false;"
                                       value="取消">
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <th><b>卡片图</b></th>
                    </tr>
                    <tr>
                        <td>
                            <div style="text-align: center;">
                                <input type="hidden" name="card_img" id="card_img" value="">
                                <a href="javascript:upload_one_image('图片上传','#card_img');">
                                    <img src="__TMPL__Public/assets/images/default-thumbnail.png" id="card_img-preview"
                                         width="135" style="cursor: hand"/>
                                </a>
                                <input type="button" class="btn btn-small"
                                       onclick="$('#card_img-preview').attr('src','__TMPL__Public/assets/images/default-thumbnail.png');$('#card_img').val('');return false;"
                                       value="取消">
                            </div>
                        </td>
                    </tr>

                    <tr>
                        <th><b>排序 : </b>
                            <input type="text" name="sort" value="">
                        </th>
                    </tr>
                    <tr>
                        <th>
                            <b>优惠券状态 : </b>
                            <label class="radio inline" ><input type="radio" name="use_flg"
                                                                                 value="1"
                                                                                 checked/>{:L('ENABLED')}</label>
                            <label class="radio inline" ><input type="radio" name="use_flg"
                                                                                  value="0">{:L('DISABLED')}</label>
                        </th>
                    </tr>
                </table>
            </div>
        </div>
        <div class="form-actions">
            <button class="btn btn-primary js-ajax-submit" type="submit">提交</button>
            <a class="btn" href="{:U('PromotionCoupon/index')}">返回</a>
        </div>
    </form>
</div>

<script type="text/javascript" src="__PUBLIC__/js/common.js"></script>
<script type="text/javascript">
    //编辑器路径定义
    var editorURL = GV.WEB_ROOT;
</script>
<script type="text/javascript" src="__PUBLIC__/js/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="__PUBLIC__/js/ueditor/ueditor.all.min.js"></script>
<script type="text/javascript">


    $(function () {

        /////---------------------
        Wind.use('validate', 'ajaxForm', 'artDialog', function () {
            //javascript

            //编辑器
            editorcontent = new baidu.editor.ui.Editor();
            editorcontent.render('content');
            try {
                editorcontent.sync();
            } catch (err) {
            }
            //增加编辑器验证规则
            jQuery.validator.addMethod('editorcontent', function () {
                try {
                    editorcontent.sync();
                } catch (err) {
                }
                return editorcontent.hasContents();
            });
            var form = $('form.js-ajax-forms');
            //ie处理placeholder提交问题
            if ($.browser && $.browser.msie) {
                form.find('[placeholder]').each(function () {
                    var input = $(this);
                    if (input.val() == input.attr('placeholder')) {
                        input.val('');
                    }
                });
            }

            var formloading = false;
            //表单验证开始
            form.validate({
                //是否在获取焦点时验证
                onfocusout: false,
                //是否在敲击键盘时验证
                onkeyup: false,
                //当鼠标掉级时验证
                onclick: false,
                //验证错误
                showErrors: function (errorMap, errorArr) {
                    //errorMap {'name':'错误信息'}
                    //errorArr [{'message':'错误信息',element:({})}]
                    try {
                        $(errorArr[0].element).focus();
                        art.dialog({
                            id: 'error',
                            icon: 'error',
                            lock: true,
                            fixed: true,
                            background: "#CCCCCC",
                            opacity: 0,
                            content: errorArr[0].message,
                            cancelVal: '确定',
                            cancel: function () {
                                $(errorArr[0].element).focus();
                            }
                        });
                    } catch (err) {
                    }
                },
                //验证规则
                rules: {
//                    'activity_title': {
//                        required: 1
//                    },
//                    'activity_detail': {
//                        editorcontent: true
//                    }
                },
                //验证未通过提示消息
                messages: {
//                    'activity_title': {
//                        required: '请输入标题'
//                    },
//                    'activity_detail': {
//                        editorcontent: '内容不能为空'
//                    }
                },
                //给未通过验证的元素加效果,闪烁等
                highlight: false,
                //是否在获取焦点时验证
                onfocusout: false,
                //验证通过，提交表单
                submitHandler: function (forms) {
                    if (formloading)
                        return;
                    $(forms).ajaxSubmit({
                        url: form.attr('action'), //按钮上是否自定义提交地址(多按钮情况)
                        dataType: 'json',
                        beforeSubmit: function (arr, $form, options) {
                            formloading = true;
                        },
                        success: function (data, statusText, xhr, $form) {
                            formloading = false;
                            if (data.status) {
                                setCookie("refersh_time", 1);
                                //添加成功
                                Wind.use("artDialog", function () {
                                    art.dialog({
                                        id: "succeed",
                                        icon: "succeed",
                                        fixed: true,
                                        lock: true,
                                        background: "#CCCCCC",
                                        opacity: 0,
                                        content: data.info,
                                        button: [{
                                            name: '继续添加？',
                                            callback: function () {
                                                reloadPage(window);
                                                return true;
                                            },
                                            focus: true
                                        }, {
                                            name: '返回列表页',
                                            callback: function () {
                                                location = "{:U('PromotionCoupon/index')}";
                                                return true;
                                            }
                                        }]
                                    });
                                });
                            } else {
                                artdialog_alert(data.info);
                            }
                        }
                    });
                }
            });
        });
        ////-------------------------
    });
</script>
</body>
</html>